<template>
  <div class="shop-info">
    <div class="info-content">
      <section class="section">
        <h3 class="section-title">配送信息</h3>
        <div class="delivery">
          <div>
            <span class="delivery-icon">{{info.description}}</span>
            <span>由商家配送提供配送，约 {{info.deliveryTime}} 分钟送达，距离 {{info.distance}}</span>
          </div>
          <div class="delivery-money">配送费￥{{info.deliveryPrice}}</div>
        </div>
      </section>
      <div class="split"></div>
      <section class="section">
        <h3 class="section-title">活动与服务</h3>
        <div class="activity">
          <div class="activity-item" :class="supportClasses[support.type]" 
          v-for="(support, index) in info.supports" :key="index">
            <span class="content-tag">
              <span class="mini-tag">{{support.name}}</span>
            </span>
            <span class="activity-content">{{support.content}}</span>
          </div>
        </div>
      </section>
      <div class="split"></div>
      <section class="section">
        <h3 class="section-title">商家实景</h3>
        <div class="pic-wrapper">
          <ul class="pic-list" ref="picUl">
            <li class="pic-item" v-for="(pic, index) in info.pics" :key="index">
              <img width="120" height="90" :src="pic"/>
            </li>
          </ul>
        </div>
      </section>
      <div class="split"></div>
      <section class="section">
        <h3 class="section-title">商家信息</h3>
        <ul class="detail">
          <li><span class="bold">品类</span> <span>{{info.category}}</span></li>
          <li><span class="bold">商家电话</span> <span>{{info.phone}}</span></li>
          <li><span class="bold">地址</span> <span>{{info.address}}</span></li>
          <li><span class="bold">营业时间</span> <span>{{info.workTime}}</span></li>
        </ul>
      </section>
    </div>
  </div>
</template>



<script>
  import BScroll from 'better-scroll'
  import {mapState} from 'vuex'

  export default {
    name: "ShopInfo",
    data() {
      return {
        supportClasses: ['activity-green', 'activity-red', 'activity-orange']
      }
    },
    mounted() {
      // 如果数据还没有，直接结束
      if (!this.info.pics) {
        return
      }
      // 数据有了，可以创建 BScroll 对象形成滑动
      this._initScroll()
      // this.$nextTick(() => {
      // })
    },
    methods: {
      _initScroll() {
        new BScroll('.shop-info')

        // 动态计算 ul 的宽度
        // const ul = this.$refs.picUl
        // const liWidth = 120
        // const space = 6
        // const count = this.info.pics.length
        // ul.style.width = (liWidth + space) * count - space + 'px'
        
        new BScroll('.pic-wrapper', {
          scrollX: true, // 水平滑动
        })
      }
    },
    watch: {
      info() { // 刷新流程 -> 更新数据
        this.$nextTick(() => { // 异步创建
          this._initScroll()
      })
      }
    },
    computed: {
      ...mapState(['info'])
    },
    components: {},
  };
</script>

<style lang="less">
  @import '../../../common/less/mixins.less';
  .shop-info {
    position: absolute;
    // top: 195px;
    top: 248px;
    bottom: 0;
    left: 0;
    width: 100%;
    background: #fff;
    overflow: hidden;
    .section {
      position: relative;
      padding: 16px 14px 14px;
      font-size: 16px;
      background-color: #fff;
      color: #666;
      border-bottom: 1px solid #eee;
      .section-title {
        color: #000;
        font-weight: 700;
        line-height: 16px;
        >.iconfont {
          float: right;
          line-height: 16px;
        }
      }
      .delivery {
        margin-top: 16px;
        font-size: 13px;
        line-height: 18px;
        .delivery-icon {
          display: inline-block;
          width: 55px;
          margin-right: 10px;
          padding: 1px 0;
          font-size: 11px;
          text-align: center;
          color: #fff;
          background-color: #0097ff;
          border-radius: 4px;
        }
        .delivery-money {
          margin-top: 5px;
        }
      }
      .activity {
        margin-top: 16px;
        .activity-item {
          display: flex;
          align-items: center;
          margin-bottom: 12px;
          font-size: 13px;
          &.activity-green {
            .content-tag {
              background-color: rgba(112, 188, 70, 1);
            }
          }
          &.activity-red {
            .content-tag {
              background-color: rgba(240, 115, 115, 1);
            }
          }
          &.activity-orange {
            .content-tag {
              background-color: rgba(241, 136, 79, 1);
            }
          }
          .content-tag {
            position: relative;
            display: inline-block;
            width: 36px;
            height: 18px;
            margin-right: 10px;
            border-radius: 2px;
            color: #fff;
            font-style: normal;
            .mini-tag {
              position: absolute;
              left: 0;
              top: 0;
              right: -100%;
              bottom: -100%;
              font-size: 24px;
              transform: scale(.5);
              transform-origin: 0 0;
              display: flex;
              justify-content: center;
              align-items: center;
            }
          }
        }
      }
      .pic-wrapper {
        width: 100%;
        margin-top: 16px;
        overflow: hidden;
        white-space: nowrap;
        .pic-list {
          font-size: 0;
          // 自适应宽度
          width: fit-content;
          .pic-item {
            display: inline-block;
            width: 120px;
            height: 90px;
            margin-right: 6px;
            &:last-child {
              margin: 0;
            }
          }
        }
      }
      .detail {
        margin-bottom: -16px;
        >li {
          display: flex;
          justify-content: space-between;
          align-items: center;
          margin-right: -10px;
          padding: 16px 12px 16px 0;
          line-height: 16px;
          .bottom-border-1px(#ddd);
          font-size: 13px;
          .bold {
            font-weight: 700;
            color: #333;
          }
          &:last-child {
            border: none;
          }
        }
      }
    }
    .split {
      width: 100%;
      height: 16px;
      border-top: 1px solid rgba(7, 17, 27, .1);
      border-bottom: 1px solid rgba(7, 17, 27, .1);
      background: #f3f5f7;
    }
  }
</style>
